let xLabel = [];
let jslData = [];
let cslData = [];
let chartStatus = 0;
const option1 = {
  colors: ["#397bff", "#5bbde1"],
  tooltip: {
    trigger: "axis",
    backgroundColor: "transparent",
    axisPointer: {
      lineStyle: {
        color: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: "rgba(126,199,255,0)", // 0% 处的颜色
            },
            {
              offset: 0.5,
              color: "rgba(126,199,255,1)", // 100% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(126,199,255,0)", // 100% 处的颜色
            },
          ],
          global: false, // 缺省为 false
        },
      },
    },
    formatter: (p) => {
      let dom = `<div style="width: 79px;
	height: 50px;;color:#fff;position: relative;">
        <svg style="position: absolute;top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);" class="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="71" viewBox="0 0 84 55">
      <defs>
        <style>
          .cls-1 {
            fill: #07172c;
            fill-opacity: 0.8;
            stroke: #a7d8ff;
            stroke-linejoin: round;
            stroke-opacity: 0.2;
            stroke-width: 1px;
            fill-rule: evenodd;
          }

        </style>
      </defs>
      <path id="矩形_419" data-name="矩形 419" class="cls-1" d="M266,595h74v50H266V624.046L261,620l5-3.984V595Z"
        transform="translate(-258.5 -592.5)" />
    </svg>
        <div style="padding: 4px 8px 4px 14px;display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;position: relative;z-index: 1;">
            <div style="margin-bottom: 4px;width:100%;display:${
          p[0] ? "flex" : "none"
      };justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[0] ? p[0].seriesName : ""}</span>
                <span style="font-size:14px;color:#fff;">${p[0] ? p[0].data : ""}</span>
            </div>
            <div style="width:100%;height:100%;display:${
          p[1] ? "flex" : "none"
      };justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[1] ? p[1].seriesName : ""}</span>
                <span style="font-size:14px;color:#fff;">${p[1] ? p[1].data : ""}</span>
            </div>
        </div>
    </div>`;
      return dom;
    },
  },
  legend: {
    align: "left",
    right: "4%",
    top: "3%",
    textStyle: {
      color: "#90A1B5",
      fontSize: 12,
    },
    icon: "rect",
    itemGap: 25,
    itemWidth: 19,
    itemHeight: 5,
    data: [
      {
        name: "进水量",
      },
      {
        name: "出水量",
      },
    ],
  },
  grid: {
    top: "15%",
    left: "7%",
    right: "4%",
    bottom: "10%",
    // containLabel: true
  },
  xAxis: [
    {
      type: "category",
      boundaryGap: false,
      axisLine: {
        //坐标轴轴线相关设置。数学上的x轴
        show: true,
        lineStyle: {
          color: "#1e4283",
          width: 3,
        },
      },
      axisLabel: {
        //坐标轴刻度标签的相关设置
        textStyle: {
          color: "#90A1B5",
          // padding: 16,
          fontSize: 12,
        },
        formatter: function (data) {
          return data;
        },
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: "#192a44",
        },
      },
      axisTick: {
        show: false,
      },
      data: xLabel,
    },
  ],
  yAxis: [
    {
      name: "m³/h",
      // boundaryGap: ["50%", "50%"],
      nameTextStyle: {
        color: "#90A1B5",
        fontSize: 12,
        padding: 10,
      },
      min: 0,
      splitLine: {
        show: true,
        lineStyle: {
          color: "#1E2E50",
        },
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#233653",
        },
      },
      axisLabel: {
        show: true,
        textStyle: {
          color: "#90A1B5",
        },
        formatter: function (value) {
          if (value === 0) {
            return value;
          }
          return value;
        },
      },
      axisTick: {
        show: false,
      },
    },
  ],
  series: [
    {
      name: "进水量",
      type: "line",
      symbol: "circle", // 默认是空心圆（中间是白色的），改成实心圆
      showAllSymbol: true,
      symbolSize: 0,
      smooth: true,
      lineStyle: {
        normal: {
          width: 2,
          color: "rgba(57, 123, 255, 1)", // 线条颜色
        },
        borderColor: "rgba(0,0,0,.4)",
      },
      itemStyle: {
        color: "rgba(57, 123, 255, 1)",
        borderColor: "#646ace",
        borderWidth: 0,
      },
      tooltip: {
        show: true,
      },
      areaStyle: {
        //区域填充样式
        normal: {
          //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: "rgba(57, 123, 255, 0.4)",
              },
              {
                offset: 1,
                color: "rgba(57, 123, 255, 0)",
              },
            ],
            false
          ),
          // shadowColor: "rgba(25,163,223, 0.5)", //阴影颜色
          // shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
      },
      data: jslData,
    },
    {
      name: "出水量",
      type: "line",
      symbol: "circle", // 默认是空心圆（中间是白色的），改成实心圆
      showAllSymbol: true,
      symbolSize: 0,
      smooth: true,
      lineStyle: {
        normal: {
          width: 2,
          color: "rgba(91, 189, 225, 1)", // 线条颜色
        },
        borderColor: "rgba(0,0,0,.4)",
      },
      itemStyle: {
        color: "rgba(91, 189, 225, 1)",
        borderColor: "#646ace",
        borderWidth: 0,
      },
      tooltip: {
        show: true,
      },
      areaStyle: {
        //区域填充样式
        normal: {
          //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: "rgba(91, 189, 225, 0.4)",
              },
              {
                offset: 1,
                color: "rgba(91, 189, 225, 0)",
              },
            ],
            false
          ),
          // shadowColor: "rgba(10,219,250, 0.5)", //阴影颜色
          // shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
      },
      data: cslData,
    },
  ],
};

let timer1 = null;
let len = 0;
function showTooltip1() {
  timer1 = setInterval(() => {
    if (len === xLabel.length) {
      len = 0;
    }
    myChart.dispatchAction({
      type: "showTip",
      seriesIndex: 0,
      dataIndex: len,
    });
    len++;
  }, 1000);
}
showTooltip1();

let xLabel2 = [];
let tjcData2 = [];
let xhcData2 = [];
let qscData2 = [];

const option2 = {
  colors: ["#397bff", "#5bbde1"],
  tooltip: {
    trigger: "axis",
    backgroundColor: "transparent",
    axisPointer: {
      lineStyle: {
        color: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: "rgba(126,199,255,0)", // 0% 处的颜色
            },
            {
              offset: 0.5,
              color: "rgba(126,199,255,1)", // 100% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(126,199,255,0)", // 100% 处的颜色
            },
          ],
          global: false, // 缺省为 false
        },
      },
    },
    formatter: (p) => {
      let dom = `<div style="width: 79px;
	height: 75px;;color:#fff;position: relative;">
        <svg style="position: absolute;top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);" class="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="71" viewBox="0 0 84 55">
      <defs>
        <style>
          .cls-1 {
            fill: #07172c;
            fill-opacity: 0.8;
            stroke: #a7d8ff;
            stroke-linejoin: round;
            stroke-opacity: 0.2;
            stroke-width: 1px;
            fill-rule: evenodd;
            display: none;
          }

        </style>
      </defs>
      <path id="矩形_419" data-name="矩形 419" class="cls-1" d="M266,595h74v50H266V624.046L261,620l5-3.984V595Z"
        transform="translate(-258.5 -592.5)" />
    </svg>
        <div style="padding: 4px 8px 4px 14px;display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;position: relative;z-index: 1;">
            <div style="margin-bottom: 4px;width:100%;display:${
          p[0] ? "flex" : "none"
      };justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[0] ? p[0].seriesName : ""}</span>
                <span style="font-size:14px;color:#fff;">${p[0] ? p[0].data : ""}</span>
            </div>
            <div style="width:100%;height:100%;display:${
          p[1] ? "flex" : "none"
      };justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[1] ? p[1].seriesName : ""}</span>
                <span style="font-size:14px;color:#fff;">${p[1] ? p[1].data : ""}</span>
            </div>
            <div style="width:100%;height:100%;display:${
          p[2] ? "flex" : "none"
      };justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[2] ? p[2].seriesName : ""}</span>
                <span style="font-size:14px;color:#fff;">${p[2] ? p[2].data : ""}</span>
            </div>
        </div>
    </div>`;
      return dom;
    },
  },
  legend: {
    align: "left",
    right: "4%",
    top: "3%",
    textStyle: {
      color: "#90A1B5",
      fontSize: 12,
    },
    icon: "rect",
    itemGap: 25,
    itemWidth: 19,
    itemHeight: 5,
    data: [
      {
        name: "调节池",
      },
      {
        name: "沉淀池",
      },
      {
        name: "清水池",
      },
    ],
  },
  grid: {
    top: "15%",
    left: "7%",
    right: "4%",
    bottom: "10%",
    // containLabel: true
  },
  xAxis: [
    {
      type: "category",
      boundaryGap: false,
      axisLine: {
        //坐标轴轴线相关设置。数学上的x轴
        show: true,
        lineStyle: {
          color: "#1e4283",
          width: 3,
        },
      },
      axisLabel: {
        //坐标轴刻度标签的相关设置
        textStyle: {
          color: "#90A1B5",
          // padding: 16,
          fontSize: 12,
        },
        formatter: function (data) {
          return data;
        },
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: "#192a44",
        },
      },
      axisTick: {
        show: false,
      },
      data: xLabel,
    },
  ],
  yAxis: [
    {
      name: "m",
      // boundaryGap: ["50%", "50%"],
      nameTextStyle: {
        color: "#90A1B5",
        fontSize: 12,
        padding: 10,
      },
      min: 0,
      splitLine: {
        show: true,
        lineStyle: {
          color: "#1E2E50",
        },
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#233653",
        },
      },
      axisLabel: {
        show: true,
        textStyle: {
          color: "#90A1B5",
        },
        formatter: function (value) {
          if (value === 0) {
            return value;
          }
          return value;
        },
      },
      axisTick: {
        show: false,
      },
    },
  ],
  series: [
    {
      name: "调节池",
      type: "line",
      symbol: "circle", // 默认是空心圆（中间是白色的），改成实心圆
      showAllSymbol: true,
      symbolSize: 0,
      smooth: true,
      lineStyle: {
        normal: {
          width: 2,
          color: "rgba(57, 123, 255, 1)", // 线条颜色
        },
        borderColor: "rgba(0,0,0,.4)",
      },
      itemStyle: {
        color: "rgba(57, 123, 255, 1)",
        borderColor: "#646ace",
        borderWidth: 0,
      },
      tooltip: {
        show: true,
      },
      areaStyle: {
        //区域填充样式
        normal: {
          //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: "rgba(57, 123, 255, 0.4)",
              },
              {
                offset: 1,
                color: "rgba(57, 123, 255, 0)",
              },
            ],
            false
          ),
          // shadowColor: "rgba(25,163,223, 0.5)", //阴影颜色
          // shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
      },
      data: tjcData2,
    },
    {
      name: "沉淀池",
      type: "line",
      symbol: "circle", // 默认是空心圆（中间是白色的），改成实心圆
      showAllSymbol: true,
      symbolSize: 0,
      smooth: true,
      lineStyle: {
        normal: {
          width: 2,
          color: "rgba(91, 189, 225, 1)", // 线条颜色
        },
        borderColor: "rgba(0,0,0,.4)",
      },
      itemStyle: {
        color: "rgba(91, 189, 225, 1)",
        borderColor: "#646ace",
        borderWidth: 0,
      },
      tooltip: {
        show: true,
      },
      areaStyle: {
        //区域填充样式
        normal: {
          //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: "rgba(91, 189, 225, 0.4)",
              },
              {
                offset: 1,
                color: "rgba(91, 189, 225, 0)",
              },
            ],
            false
          ),
          // shadowColor: "rgba(10,219,250, 0.5)", //阴影颜色
          // shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
      },
      data: xhcData2,
    },
    {
      name: "清水池",
      type: "line",
      symbol: "circle", // 默认是空心圆（中间是白色的），改成实心圆
      showAllSymbol: true,
      symbolSize: 0,
      smooth: true,
      lineStyle: {
        normal: {
          width: 2,
          color: "rgb(255, 235, 103, 1)", // 线条颜色
        },
        borderColor: "rgba(0,0,0,.4)",
      },
      itemStyle: {
        color: "rgb(255, 235, 103, 1)",
        borderColor: "#646ace",
        borderWidth: 0,
      },
      tooltip: {
        show: true,
      },
      areaStyle: {
        //区域填充样式
        normal: {
          //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: "rgb(255, 235, 103, 0.4)",
              },
              {
                offset: 1,
                color: "rgb(255, 235, 103, 0)",
              },
            ],
            false
          ),
          // shadowColor: "rgba(10,219,250, 0.5)", //阴影颜色
          // shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
      },
      data: qscData2,
    },
  ],
};

let timer2 = null;
let len2 = 0;
function showTooltip2() {
  timer2 = setInterval(() => {
    if (len === xLabel2.length) {
      len = 0;
    }
    myChart.dispatchAction({
      type: "showTip",
      seriesIndex: 0,
      dataIndex: len,
    });
    len2++;
  }, 1000);
}
showTooltip2();

const chartEl = document.getElementById("chart");
const myChart = echarts.init(chartEl);

myChart.setOption(option1);

const monitorBtn = document.querySelector(".monitor .btn");
const equipmentDetailBtn = document.querySelector(".equipment-detail .btn");
const monitorEl = document.querySelector(".monitor");
const equipmentDetailEl = document.querySelector(".equipment-detail");
const chartName1 = document.querySelector(".day-deal-echart .title .name");
const chartName2 = document.querySelector(".day-deal-echart .title .chart-name");

// 点击视频监控按钮
monitorBtn.addEventListener("click", () => {
  // 1. 切换到设备详情
  monitorEl.style.display = "none";
  equipmentDetailEl.style.display = "block";
});

// 点击设备详情按钮
equipmentDetailBtn.addEventListener("click", () => {
  // 1. 切换视频监控与设备详情
  monitorEl.style.display = "block";
  equipmentDetailEl.style.display = "none";
});

chartName1.addEventListener("click", () => {
  // 1. 切换图表部分的标题高亮
  chartName2.style.fontSize = "18px";
  chartName2.style.textShadow = "none";
  chartName2.style.color = "#b3cce9";
  chartName1.style.fontSize = "22px";
  chartName1.style.textShadow = "0 2px 20px rgba(95, 207, 254, 0.8)";
  chartName1.style.color = "#fff";

  // 2. 切换echarts图
  myChart.clear();
  myChart.setOption(option1);
  len = len2 = 0;
  chartStatus = 0
});

chartName2.addEventListener("click", () => {
  // 1. 切换图表部分的标题高亮
  chartName1.style.fontSize = "18px";
  chartName1.style.textShadow = "none";
  chartName1.style.color = "#b3cce9";
  chartName2.style.fontSize = "22px";
  chartName2.style.textShadow = "0 2px 20px rgba(95, 207, 254, 0.8)";
  chartName2.style.color = "#fff";

  // 2. 切换echarts图
  myChart.clear();
  myChart.setOption(option2);
  len = len2 = 0;
  chartStatus = 1
});

const liveDataMoreEl = document.querySelector(".live-data .more");
const equipmentStatusMoreEl = document.querySelectorAll(".equipment-status .moreData");
const liveDataDiaEl = document.querySelector(".live-data-dialog"); // 实时数据弹窗
const equipmentStatusDiaEl = document.querySelector(".equipment-status-dialog"); // 设备运行状态弹窗
const liveDataCloseEl = document.querySelector(".live-data-dialog .close"); // 实时数据弹窗关闭按钮
const equipmentStatusCloseEl = document.querySelector(".equipment-status-dialog .close"); // 设备运行状态弹窗关闭按钮
const edit0Data = document.querySelector(".edit0");
const edit1Data = document.querySelector(".edit1");
const edit2Data = document.querySelector(".edit2");
const editModal = document.querySelector(".edit-dialog")
const editClose = document.querySelector(".edit-dialog .close")
const editSave = document.querySelector(".checked");
liveDataMoreEl.addEventListener("click", () => {
  liveDataDiaEl.style.display = "block";
});

editSave.addEventListener("click", () => {
  saveEdit();
  editModal.style.display = "none";
});

edit0Data.addEventListener("click", () => {
  editModal.style.display = "block";
  getEditData(0);
});
edit1Data.addEventListener("click", () => {
  editModal.style.display = "block";
  getEditData(1);
});
edit2Data.addEventListener("click", () => {
  editModal.style.display = "block";
  getEditData(3);
});
Array.from(equipmentStatusMoreEl).forEach((item) => {
  item.addEventListener("click", () => {
    equipmentStatusDiaEl.style.display = "block";
  });
});

liveDataCloseEl.addEventListener("click", () => {
  liveDataDiaEl.style.display = "none";
});

editClose.addEventListener("click", () => {
  editModal.style.display = "none";
});

equipmentStatusCloseEl.addEventListener("click", () => {
  equipmentStatusDiaEl.style.display = "none";
});
